#include "PrototypeTimer.hpp"
#include "Keyboard.hpp"

static volatile int tick = 0;
void interrupt_function_call()
{
    ++tick;
}
END_OF_FUNCTION(interrupt_function_call);

void PrototypeTimer::Run()
{
}

//////////////////////////////////////////////////////////////////////////
void PrototypeTimer::Render(BITMAP* aBuffer)
{
    int tickPerSec = 1000; 
    int tickPerMin = tickPerSec*60;
    int tickPerHour = tickPerMin*60;
    int currentTick = tick;
    int hour = (int)currentTick / tickPerHour;
    currentTick -= hour*tickPerHour;
    int minute = (int)currentTick / tickPerMin;
    currentTick -= minute*tickPerMin;
    int sec = (int)currentTick / tickPerSec;
    currentTick -= sec*tickPerSec;
    char text[64] = { '\0' };
    sprintf(text,"%.2i : %.2i : %.2i : %.3i", hour, minute, sec, currentTick);
    textout_ex(aBuffer, font, text, 160, 100, (255,255,255), NULL);    
}

//////////////////////////////////////////////////////////////////////////
void PrototypeTimer::Init()
{
    int iSpeed = 1;
    mpTimer.reset( new timer::Timer(interrupt_function_call, iSpeed) );
    LOCK_VARIABLE(tick);
    LOCK_FUNCTION(interrupt_function_call);
    tick = 0;
    mpTimer->Register();
}

//////////////////////////////////////////////////////////////////////////
void PrototypeTimer::DeInit()
{
    mpTimer->Unregister();
    mpTimer.reset();
}